package com.py.credit.controller;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.SysUser;
import com.py.credit.service.DataMapCollectionService;
import com.py.credit.service.RemoteBaseService;
import com.py.framework.core.helper.HttpServletHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据归集大屏controller
 */
@Controller
@RequestMapping(value = "/dataMapCollection")
public class DataMapCollectionController {

    @Autowired
    private DataMapCollectionService dataMapCollectionService;

    @Autowired
    private RemoteBaseService remoteBaseService;

    /**
     * 数据归集（部门报送排名TOP10)与部门归集数量占比
     * @param areaCode 地区编码
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getOfficeUploadTop.json")
    public RestServiceResult<Object> getOfficeUploadTop10(String areaCode, HttpServletRequest request){
        RestServiceResult<Object> result = new RestServiceResult<Object>();

        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("DataMapCollectionController#getOfficeUploadTop", "未获取到当前用户信息！");
                return result;
            }

            List<LinkedHashMap> data = dataMapCollectionService.getOfficeUploadTop10();
            //如果没有数据的话给一个空数据
            if(null==data || data.size()==0){
                data=new ArrayList<LinkedHashMap>();
                for(int i=0;i<10;i++){
                    LinkedHashMap linkedHashMap=new LinkedHashMap();
                    linkedHashMap.put("gjNum",0);
                    linkedHashMap.put("officeName","--");
                    data.add(linkedHashMap);
                }
            }

            result.setData(data);
            return result;
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
    }

    /**
     * 部门数据归集趋势
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getOfficeRateOfIncrease.json")
    public RestServiceResult<Object> getOfficeRateOfIncrease(HttpServletRequest request){
        RestServiceResult<Object> result = new RestServiceResult<Object>();

        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("DataMapCollectionController#getOfficeRateOfIncrease", "未获取到当前用户信息！");
                return result;
            }

            result.setData(dataMapCollectionService.getOfficeRateOfIncrease());
            return result;
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
    }

    /**
     * 区域归集数据量增长率
     * @param areaCode 地区编码
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAreaRateOfIncrease.json")
    public RestServiceResult<Object> getAreaRateOfIncrease( HttpServletRequest request){
        RestServiceResult<Object> result = new RestServiceResult<Object>();

        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("DataMapCollectionController#getAreaRateOfIncrease", "未获取到当前用户信息！");
                return result;
            }

            result.setData(dataMapCollectionService.getAreaRateOfIncrease());
            return result;
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
    }

    /**
     * 区域报送排名
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAreaUpload.json")
    public RestServiceResult<Object> getAreaUpload(HttpServletRequest request){
        RestServiceResult<Object> result = new RestServiceResult<Object>();

        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("DataMapCollectionController#getAreaUpload", "未获取到当前用户信息！");
                return result;
            }

            List<LinkedHashMap> data = dataMapCollectionService.getAreaUpload();

            result.setData(data);
            return result;
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
    }

    /**
     * 区域数据归集量12个月
     * @param areaCode 地区编码
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAreaUploadByMonth.json")
    public RestServiceResult<Object> getAreaUploadByMonth(HttpServletRequest request){
        RestServiceResult<Object> result = new RestServiceResult<Object>();

        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("DataMapCollectionController#getAreaUploadByMonth", "未获取到当前用户信息！");
                return result;
            }

            List<Map> data = dataMapCollectionService.getAreaUploadByMonth();
            result.setData(data);
            return result;
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
    }

    /**
     * 信用主体增长率
     * @param areaCode 地区编码
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getDataSourceRateOfIncrease.json")
    public RestServiceResult<Object> getDataSourceRateOfIncrease(String areaCode, HttpServletRequest request){
        RestServiceResult<Object> result = new RestServiceResult<Object>();

        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("DataMapCollectionController#getDataSourceRateOfIncrease", "未获取到当前用户信息！");
                return result;
            }

            result.setData(dataMapCollectionService.getDataSourceRateOfIncrease());
            return result;
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
    }

    /**
     * 信用主体占比
     * @param areaCode 地区编码
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getDataSource.json")
    public RestServiceResult<Object> getDataSource(String areaCode, HttpServletRequest request){
        RestServiceResult<Object> result = new RestServiceResult<Object>();

        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("DataMapCollectionController#getDataSource", "未获取到当前用户信息！");
                return result;
            }

            result.setData(dataMapCollectionService.getDataSource());
            return result;
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
    }

    /**
     * 数据采集方式
     * @param areaCode 地区编码
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getDataCollectType.json")
    public RestServiceResult<Object> getDataCollectType(String areaCode, HttpServletRequest request){
        RestServiceResult<Object> result = new RestServiceResult<Object>();

        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("DataMapCollectionController#getDataCollectType", "未获取到当前用户信息！");
                return result;
            }

            List<LinkedHashMap> data = dataMapCollectionService.getDataCollectType();

            //如果没有数据的话给一个空数据
            if(null==data || data.size()==0){
                data=new ArrayList<LinkedHashMap>();
                for (int i=0;i<5;i++){
                    LinkedHashMap linkedHashMap=new LinkedHashMap();
                    linkedHashMap.put("gjNum",0);
                    linkedHashMap.put("collectType","--");
                    data.add(linkedHashMap);
                }
            }

            result.setData(data);
            return result;
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
    }
    
    /**
     * 信用主体占比
     * @param areaCode 地区编码
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/collectionByMonth.json")
    public RestServiceResult<LinkedHashMap> collectionByMonth(String areaCode, HttpServletRequest request){
        RestServiceResult<LinkedHashMap> result = new RestServiceResult<LinkedHashMap>();

        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("DataMapCollectionController#getDataSource", "未获取到当前用户信息！");
                return result;
            }

            result.setData(dataMapCollectionService.collectionByMonth());
            return result;
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
    }
    
    /**
     * 信用主体占比
     * @param areaCode 地区编码
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/collectionByRelateStatus.json")
    public RestServiceResult<Object> collectionByRelateStatus(HttpServletRequest request){
        RestServiceResult<Object> result = new RestServiceResult<Object>();

        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("DataMapCollectionController#getDataSource", "未获取到当前用户信息！");
                return result;
            }

            result.setData(dataMapCollectionService.collectionByRelateStatus());
            return result;
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
    }
    
    /**
     * 信用主体占比
     * @param areaCode 地区编码
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/collectionByTarget.json")
    public RestServiceResult<Object> collectionByTarget(String type, HttpServletRequest request){
        RestServiceResult<Object> result = new RestServiceResult<Object>();

        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("DataMapCollectionController#getDataSource", "未获取到当前用户信息！");
                return result;
            }

            result.setData(dataMapCollectionService.collectionByTarget(type));
            return result;
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
    }
    
}
